<?php
/**
 * Created by PhpStorm.
 * User: sofwu
 * Date: 2019/1/11
 * Time: 下午3:13
 */
namespace App\Http\Controllers\Api\V1;


use App\Http\Transformer\GoodsTransformer;
use App\Model\Goods;

class GoodsController extends Controller
{
    /**
     * 获取产品
     */
    public function goodsList()
    {
        $goods = Goods::where(['enabled'=>1])->orderBy('sort')->get();

        return $this->response->collection($goods, new GoodsTransformer);
    }


    /**
     * 上传图片
     */
    public function storePic()
    {
        $payload = app('request')->only('path', 'goods_id');
        $goods = Goods::find($payload['goods_id']);
        $mediaItems = $goods->getMedia();

        if($mediaItems && count($mediaItems)){
            $mediaItems[0]->delete();
        }
        $goods->addMedia($payload['path'])->toMediaCollection();
        return $goods->getFirstMediaUrl();
    }
}